function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  return [year, month, day].map(formatNumber).join('-');
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//上传文件
function upload_file(url, filePath, name, formData, success, fail) {
  console.log('a=' + filePath)
  wx.uploadFile({
    url: url,
    filePath: filePath,
    name: name,
    header: {
      'content-type': 'multipart/form-data'
    }, // 设置请求的 header
    formData: formData, // HTTP 请求中其他额外的 form data
    success: function (res) {
      console.log(res);
      if (res.statusCode == 200 && !res.data.result_code) {
        typeof success == "function" && success(res.data);
      } else {
        typeof fail == "function" && fail(res);
      }
    },
    fail: function (res) {
      console.log(res);
      typeof fail == "function" && fail(res);
    }
  })
}

function showSuccessToast(delta){

  wx.showToast({

    title: '保存成功',
    icon: 'success',
    duration: 1500,

    success: function () {

      setTimeout(function () {
        wx.navigateBack({
          delta: delta
        })
      }, 1500);
    }
  })

}

function showToastAndRedirect(url) {

  wx.showToast({

    title: '保存成功',
    icon: 'success',
    duration: 1500,

    success: function () {

      setTimeout(function () {
        wx.redirectTo({
          url: url,
        })
      }, 1500);
    }
  })

}

function showRejectToast(title){
  wx.showToast({
    title: title,
    image: '../../../image/my_about@2x.png',
    duration: 1500
  })
}




module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  upload_file: upload_file,
  showSuccessToast: showSuccessToast,
  showToastAndRedirect: showToastAndRedirect,
  showRejectToast: showRejectToast
}

